import com.wxp.entity.Role;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Id;
import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lewis on 2017/9/17.
 */
public class GenerateCode {
    public static void main(String args[]) throws TemplateException {
        Configuration cfg = new Configuration();
        Template template = null;
        //Writer out = new OutputStreamWriter(System.out);
        // 指定FreeMarker模板文件的位置
        try {
            Class<?> model =Role.class;
            //获取当前类所在的包
            String name = model.getName();
            //截取类短名
            String model_name = name.substring(name.lastIndexOf(".")+1);
            String model_package = name.substring(0,name.lastIndexOf("."));
            String model_package_dao = model_package.substring(0,model_package.lastIndexOf(".")+1)+"dao";
            String model_package_service = model_package.substring(0,model_package.lastIndexOf(".")+1)+"service";
            String model_package_serviceImpl = model_package.substring(0,model_package.lastIndexOf(".")+1)+"serviceImpl";
            String model_package_controller = model_package.substring(0,model_package.lastIndexOf(".")+1)+"controller";

            Map<String, String> root = new HashMap<String, String>();
            root.put("model_name", model_name);
            root.put("model_name_entity", StringUtils.uncapitalize(model_name));
            root.put("model_package_dao", model_package_dao);
            root.put("name", name);
            root.put("model_package_service", model_package_service);
            root.put("model_package_serviceImpl", model_package_serviceImpl);
            root.put("model_package_controller", model_package_controller);
            Field[] fields =  model.getDeclaredFields();

            String model_properties = "";
            for(Field f : fields){
                if(f.isAnnotationPresent(Id.class)){
                    root.put("model_key", f.getName());
                    root.put("model_key_by", StringUtils.capitalize(f.getName()));
                }
                model_properties = model_properties+"\""+f.getName()+"\",";
            }
            root.put("model_properties", model_properties);

            //dao
            cfg.setDirectoryForTemplateLoading(new File("src/template"));
            template = cfg.getTemplate("dao.ftl");
            template.process(root,  new OutputStreamWriter(generateJava(model_package_dao,model_name+"Dao.java")));


            //service
            template = cfg.getTemplate("service.ftl");
            template.process(root,  new OutputStreamWriter(generateJava(model_package_service,model_name+"Service.java")));


            //serviceImpl
            template = cfg.getTemplate("serviceImpl.ftl");
            template.process(root,  new OutputStreamWriter(generateJava(model_package_serviceImpl,model_name+"ServiceImpl.java")));

            //controller

            template = cfg.getTemplate("controller.ftl");
            template.process(root,  new OutputStreamWriter(generateJava(model_package_controller,model_name+"Controller.java")));


            //list
            template = cfg.getTemplate("list.ftl");
            template.process(root,  new OutputStreamWriter(generateJsp(StringUtils.uncapitalize(model_name),StringUtils.uncapitalize(model_name)+"List.jsp")));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static OutputStream generateJava(String packages,String name){
        System.out.print(packages.replaceAll("[.]","/"));
        String path = "/Users/lewis/Desktop/tools/"+packages.replaceAll("[.]","/");
        File file = new File(path,name);
        OutputStream out = null;
        if(file.exists()){
            System.out.println(file.getAbsolutePath());
            System.out.println(file.getName());
            System.out.println(file.length());
        }else{
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
                out = new FileOutputStream(file);
            } catch (IOException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

        return out;
    }

    public static OutputStream generateJsp(String model,String name){
        String path = "/Users/lewis/Desktop/tools/jsp/"+model;
        File file = new File(path,name);
        OutputStream out = null;
        if(file.exists()){
            System.out.println(file.getAbsolutePath());
            System.out.println(file.getName());
            System.out.println(file.length());
        }else{
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
                out = new FileOutputStream(file);
            } catch (IOException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

        return out;
    }
}
